import request from "superagent";
import {
    serverPath,
    json
} from "../../common/config.js";
import {
    prepareRequest,
    serviceInterceptor
} from "../../common/utils";
import {
    userValidator
} from "../validator/user/userValidator.js"
import validate from 'validate.js'

class LoginService {
    /**
     * @constructor
     */
    constructor() {

    }
    @serviceInterceptor()
    async loginAsync(username, password) {
        var data = { username: username, password: password };
        var result = await validate(data, userValidator);
        if (!result) {
            let req = request.post(`${serverPath}api/login`)
                .send(data)
                .accept(json);
            return await prepareRequest(req);

        }
        else {
            return result;
        }
    }
}

export default LoginService;
